package hot_100;

public class 二叉树中的最大路径和 {
    public static void main(String[] args) {

    }

    int maxx = -0x3f3f3f3f;

    public int maxPathSum(TreeNode root) {
        Dfs(root);
        return maxx;
    }

    public int Dfs(TreeNode root) {
        if (root == null)
            return 0;
        int lc = Math.max(Dfs(root.left), 0);
        int rc = Math.max(Dfs(root.right), 0);
        maxx = Math.max(maxx, lc + rc + root.val);
        return Math.max(lc, rc) + root.val;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
